package com.wanjia.seal.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.OperatorIdDuplicateException;
import com.wanjia.seal.manager.controller.ex.OperatorIdNotFoundException;
import com.wanjia.seal.manager.controller.ex.PasswordNotMatchException;
import com.wanjia.seal.manager.entity.BankInfoTB;
import com.wanjia.seal.manager.entity.OperatorUser;
import com.wanjia.seal.manager.entity.OperatorUserVoReturn;
import com.wanjia.seal.manager.mapper.BankInfoTBMapper;
import com.wanjia.seal.manager.mapper.OperatorUserMapper;
import com.wanjia.seal.manager.service.Impl.TokenServiceA;
import com.wanjia.seal.manager.service.OperatorUserService;
import com.wanjia.seal.manager.service.TokenService;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/api")
@Api(tags = "人员管理")
@Slf4j
public class OperatorUserController extends BaseController {
    @Autowired
    OperatorUserService operatorUserService;
    @Autowired
    OperatorUserMapper operatorUserMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    BankInfoTBMapper bankInfoTBMapper;

    /**
     * 操作员登陆
     * @param operatorUser 操作员ID密码
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/operator/login")
    @ApiOperation(value = "用户登陆", notes = "登陆是否成功")
    public JsonResult<Object> login(@RequestBody OperatorUser operatorUser) {
        String operatorid = operatorUser.getOperatorID();
        OperatorUser id = operatorUserMapper.selectById(operatorid);
        if (id==null){
            throw new OperatorIdNotFoundException("操作员ID不存在的异常");
        }
        String password = operatorUser.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Operatorid",operatorid);
        OperatorUser operatorUser1 = operatorUserMapper.selectOne(wrapper);
        String pwd = operatorUser1.getPassword();
        if (!s.equals(pwd)){
            throw new PasswordNotMatchException("密码错误的异常");
        }
        OperatorUser data = operatorUserService.login(operatorid,s);
        String token = tokenService.getToken(data);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("token",token);
        //jsonObject.put("operatorUser",data);
        return new JsonResult<>(OK,jsonObject);
    }

    @ResponseBody
    @PostMapping("/operator/logins")
    @ApiOperation(value = "指纹用户登陆", notes = "登陆是否成功")
    public JsonResult<Object> logins(@RequestBody OperatorUser operatorUser) {
        OperatorUser aa = operatorUserMapper.selectById("9999");
        String token = tokenService.getToken(aa);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("token",token);
        return new JsonResult<>(OK,jsonObject);
    }

    /**
     * 操作员注册模块
     * @param
     * @param
     * @return
     */
    @ResponseBody
    @LoginToken
    @PostMapping("/operator")
    @ApiOperation(value = "用户添加", notes = "添加用户是否成功")
    public JsonResult register(@RequestBody OperatorUser operatorUser){
        String operatorID = operatorUser.getOperatorID();
        String password = operatorUser.getPassword();
        String operatorName = operatorUser.getOperatorName();
        String bankID = operatorUser.getBankID();
        String status = operatorUser.getStatus();
        Integer permission = operatorUser.getPermission();
        OperatorUser id = operatorUserMapper.selectById(operatorID);
        if (id !=null){
            throw new OperatorIdDuplicateException("操作员ID被占用的异常");
        }
        operatorUser.setOperatorID(operatorID);
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        operatorUser.setPassword(s);
        operatorUser.setOperatorName(operatorName);
        operatorUser.setBankID(bankID);
        operatorUser.setStatus(status);
        operatorUser.setPermission(permission);
        operatorUser.setModifyDate(new Date());
        operatorUser.setCreator(operatorID);
        operatorUserMapper.insert(operatorUser);
        return new JsonResult(OK);
    }

    /**
     * 更新密码
     * @param
     * @param
     * @param
     * @return
     */
    @ResponseBody
    @LoginToken
    @PutMapping("/operator/id")
    @ApiOperation(value = "用户修改", notes = "修改用户是否成功")
    public JsonResult update(@RequestBody OperatorUser operatorUser){
        String operatorID = operatorUser.getOperatorID();
        String newpassword = operatorUser.getPassword();
        String operatorName = operatorUser.getOperatorName();
        String bankID = operatorUser.getBankID();
        String status = operatorUser.getStatus();
        Integer permission = operatorUser.getPermission();
        String authGrade = operatorUser.getAuthGrade();
        String checker = operatorUser.getChecker();
        String creator = operatorUser.getCreator();
        String operatorPhone = operatorUser.getOperatorPhone();
        String reserved = operatorUser.getReserved();
        OperatorUser id = operatorUserMapper.selectById(operatorID);
        if (id ==null){
            throw new OperatorIdDuplicateException("操作员不存在");
        }
        /*String pwd = id.getPassword();
        String s = DigestUtils.md5DigestAsHex(Password.getBytes());
        if (!pwd.equals(s)){
            throw new PasswordNotMatchException("密码错误");
        }*/
        operatorUser.setOperatorID(operatorID);
        String ss = DigestUtils.md5DigestAsHex(newpassword.getBytes());
        operatorUser.setPassword(ss);
        operatorUser.setBankID(bankID);
        operatorUser.setStatus(status);
        operatorUser.setPermission(permission);
        operatorUser.setAuthGrade(authGrade);
        operatorUser.setOperatorName(operatorName);
        operatorUser.setCreator(creator);
        operatorUser.setChecker(checker);
        operatorUser.setOperatorPhone(operatorPhone);
        operatorUser.setModifyDate(new Date());
        operatorUser.setReserved(reserved);
        int rows = operatorUserMapper.updateById(operatorUser);
        return new JsonResult<>(OK);
    }

    /**
     * 删除操作员
     * @param OperatorID 操作员id
     * @return
     */
    @ResponseBody
    @LoginToken
    @DeleteMapping("/operator/id")
    @ApiOperation(value = "删除用户", notes = "删除用户是否成功")
    public JsonResult delete(String OperatorID){
        Integer id = operatorUserMapper.deleteById(OperatorID);
        if (id == null){
            throw new OperatorIdDuplicateException("操作员不存在");
        }
        return new JsonResult<>(OK);
    }

    /**
     * 根据操作员ID查询操作员信息
     * @param
     * @return 操作员所有信息
     */
    @ResponseBody
    @LoginToken
    @GetMapping("/operator/id")
    @ApiOperation(value = "用ID查询用户", notes = "查询ID用户是否成功")
    public JsonResult<OperatorUser> select(String OperatorID){
        OperatorUser operatorUser = operatorUserMapper.selectById(OperatorID);
        /*if (id ==null){
            throw new OperatorIdDuplicateException("操作员不存在");
        }*/
       return new JsonResult(OK,operatorUser);
    }

   /* *//**
     * 根据操作员ID查询操作员信息
     * @param
     * @return 操作员所有信息
     *//*
    @ResponseBody
    @LoginToken
    @GetMapping("/operators/id")
    @ApiOperation(value = "用ID筛选查询用户", notes = "筛选查询ID用户是否成功")
    public JsonResult selectByOperaterID(String OperatorID, Integer pageNum, Integer pageSize){
        //OperatorUser id = operatorUserMapper.selectById(OperatorID);
        *//*if (id ==null){
            throw new OperatorIdDuplicateException("操作员不存在");
        }*//*
        //String OperatorID = operatorUser.getOperatorID();
        OperatorUserVoReturn operatorUserVoReturn = operatorUserMapper.selectByOperatorID(OperatorID, pageNum, pageSize);
        Integer rows = operatorUserMapper.selectByOperatorIDCount(OperatorID);
        JSONObject jsonObject =new JSONObject();
        jsonObject.put("OperatorUser",operatorUserVoReturn);
        jsonObject.put("total",rows);
        return new JsonResult(OK,jsonObject);
    }*/

    /**
     * 查询所有操作员信息
     * @return 操作员所有信息
     */
    @ResponseBody
    @LoginToken
    @GetMapping("/operators")
    @ApiOperation(value = "查询全部用户", notes = "查询用户是否成功")
    public JsonResult<Object> selects(String OperatorID,String BankID,Integer pageNum, Integer pageSize){
        List<OperatorUserVoReturn> operatorUser = operatorUserMapper.SelectUserPro(OperatorID,BankID,pageNum, pageSize);
        Integer rows = operatorUserMapper.selectByOperatorIDCount(OperatorID,BankID);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",rows);
        jsonObject.put("operatorUser",operatorUser);
        return new JsonResult<>(OK,jsonObject);

    }

}
